package com.example.koizzi.ad20180517_news.adepter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.koizzi.ad20180517_news.ViewNewsActivity;
import com.example.koizzi.ad20180517_news.bean.News;
import com.example.koizzi.ad20180517_news.R;

public class VerticalAdater extends RecyclerView.Adapter<VerticalAdater.ViewHolder>{
    private Context context;
    private News news;

    public VerticalAdater(Context context, News news) {
        this.context = context;
        this.news = news;
    }

    public void changeDate(News news) {
        Log.e("MyAdapter", "changData");
        this.news = news;
        notifyDataSetChanged();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        View itemView;
        ImageView newsHeadImg;
        TextView tvTitle;
        TextView tvAuthor;
        TextView tvTime;
        public ViewHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;
            newsHeadImg = itemView.findViewById(R.id.iv_head);
            tvTitle = itemView.findViewById(R.id.tv_title);
            tvAuthor = itemView.findViewById(R.id.tv_author);
            tvTime = itemView.findViewById(R.id.tv_time);
        }
    }

    @NonNull
    @Override
    public VerticalAdater.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.verti_item,parent,false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final VerticalAdater.ViewHolder holder, final int position) {
        final News.ResultBean.DataBean dataBean = news.getResult().getData().get(position);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =  new Intent();
                intent.setClass(context, ViewNewsActivity.class);
                intent.putExtra("NEWS_URL", dataBean.getUrl());
                context.startActivity(intent);
                Log.e("ver","******"+position);
            }
        });
        holder.tvTitle.setText(dataBean.getTitle());
        holder.tvAuthor.setText(dataBean.getAuthor_name());
        holder.tvTime.setText(dataBean.getDate());
        Glide.with(context).load(Uri.parse(dataBean.getThumbnail_pic_s())).into(holder.newsHeadImg);

    }

    @Override
    public int getItemCount() {
        if(news == null){
            return 0;
        }

        if(news.getResult() == null){
            return 0;
        }

        if(news.getResult().getData() == null){
            return 0;
        }

        int count = news.getResult().getData().size();

        Log.e("MyAdapter", "count = " + count);

        return count;
    }

}
